<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once("includes/connect.php");
require_once("includes/function.php");

$id = $_REQUEST["id"];
$tblname = $_REQUEST["tblname"];

$db = new myDBS;

switch ($tblname) {
    case "tbl_pet":
        $sql = "SELECT DISTINCT id, tt, receiver "
                . " FROM tbl_pet "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND id = '$id'";

        break;

    case "tbl_petition":

        $sql = "SELECT DISTINCT petition_id AS id, (inpValues * WithholdingTax) / 100 AS tt, company_name AS receiver "
                . " FROM tbl_petition "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND petition_id = '$id'";

        break;

    case "tbl_salary":
        $sql = " SELECT salary_id AS id, tax1 AS tt, tbl_init.card_id AS receiver "
                . " FROM  tbl_salary , tbl_init "
                . " WHERE approved_status =  'อนุมัติ' "
                . " AND tbl_salary.id = tbl_init.card_id"
                . " AND tax1 <> 0 "
                . " AND salary_id = '$id'";

        break;

    default:

        $sql = "SELECT DISTINCT id, tt, receiver "
                . " FROM tbl_pet "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND id = '$id'";

        break;
}

$result = $db->runSQL($sql);
$rows = $db->fetch_object($result);

$C = new Currency;

// just require TCPDF instead of FPDF
//define('FPDF_FONTPATH','fpdf/font/');
require_once('fpdf/TCPDF/tcpdf.php');
require_once('fpdf/FPDI/fpdi.php');

class PDF extends FPDI {

    /**
     * "Remembers" the template id of the imported page
     */
    var $_tplIdx;

    /**
     * include a background template for every page
     */
    function Header() {
        if (is_null($this->_tplIdx)) {
            $this->setSourceFile('form/tax1.pdf');
            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);

        /*
          //$this->AddFont('THSarabunNew','','THSarabunNew.php');
          //$this->SetFont('THSarabunNew', '', 9);
          $this->SetTextColor(255);
          $this->SetXY(60.5, 24.8);
          $this->Cell(0, 8.6, "TCPDF and FPDI");
         */
    }

    function Footer() {
        
    }

}

// initiate PDF
//Add a custom size  
//$pagelayout = array(100, 160);
$pdf = new PDF($orientation = 'P', $unit = 'mm', 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);


// add a page
$pdf->AddPage();

//$pdf->AddFont('THSarabunNew','','THSarabunNew.php');
//$pdf->SetFont("freeserif", "", 12);
$pdf->SetFont('thsarabunb', 'B', 15);

$pdf->SetXY(20, 35);
$pdf->Write(0, "สำนักงานเทศบาลนครอุดรธานี");

$pdf->SetXY(23, 43);
$pdf->Write(0, "ถนนอธิบดี ตำบลหมากแข้ง อำเภอเมือง จังหวัดอุดรธานี 4100");

$pdf->SetXY(20, 60);
if (is_numeric($rows->receiver)) {

    $pdf->Write(0, get_name_person($rows->receiver));

    $pdf->SetXY(133, 53);
    $pdf->Write(0, $rows->receiver);
} else {
    //echo $rows->receiver;
    $pdf->Write(0, $rows->receiver);
}

$pdf->SetXY(23, 69);
$pdf->Write(0, "2134/3 หมู่ 7 ต. หนองโคตร อ.เมือง จ.อุดรธานี 42234");

$pdf->SetXY(173, 202);
$pdf->Cell(25, 0, number_format($rows->tt, 2), 0, 0, 'R');
//$pdf->Write(0, number_format($rows->tt, 2));

$pdf->SetXY(173, 227);
$pdf->Cell(25, 0, number_format($rows->tt, 2), 0, 0, 'R');

$pdf->SetXY(70, 235);
$pdf->Write(0, $C->bahtThai($rows->tt));



$pdf->Output('output_cash.pdf', 'I');
?>

